package com.itcast.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.yygh.cmn.client.DictFeignClient;
import com.itcast.yygh.model.enums.DictEnum;
import com.itcast.yygh.model.model.user.Patient;
import com.itcast.yygh.user.mapper.PatientApiMapper;
import com.itcast.yygh.user.service.PatientApiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PatientApiServiceImpl extends ServiceImpl<PatientApiMapper, Patient> implements PatientApiService {

    //引入字典服务中的feign接口
    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllByUserId(Long userId) {
        //1.根据用户id查询用户下的所有就诊人的信息
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patients = baseMapper.selectList(queryWrapper);
        //2.处理就诊人对象中的省市区,需要设置省市区的名称
        patients.stream().forEach(item->{
            //需要封装其他参数
            this.packPatient(item);
        });
        return patients;
    }
    //封装就诊人其他的参数信息
    private void packPatient(Patient item) {
        //1.需要根据省、市、区的编码查询字典表中的名称
        //证件类型
        String certificatesName = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), item.getCertificatesType());
        //省名称
        String proviceName = dictFeignClient.getName(item.getProvinceCode());
        //市名称
        String cityName = dictFeignClient.getName(item.getCityCode());
        //区名称
        String districtName = dictFeignClient.getName(item.getDistrictCode());
        //完整的地址
        String fullAddress = proviceName+cityName+districtName+item.getAddress();
        //2.将获取到的名称设置到就诊人对象中
        item.getParam().put("certificatesTypeString",certificatesName);
        item.getParam().put("provinceString",proviceName);
        item.getParam().put("cityString",cityName);
        item.getParam().put("districtString",districtName);
        item.getParam().put("fullAddress",fullAddress);
    }
}
